//点击返回上一页
$(".headerCity i").on("click",function(){
    history.back(-1);
});
//获取当前城市
var htmlCity={
        city:'',
        getCity:function (){
            if(window.localStorage&&window.localStorage.currentCity!=null){
                city=localStorage.currentCity;
                localStorage.city=null;
                if($(".fixedCity")){
                    $(".fixedCity").html(city);
                }
            }else{
                htmlCity.getAddress(htmlCity.reqUrl,htmlCity.addr_success);
            }
        },
        reqUrl:'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',
        addr_success:function (_result){
            if (remote_ip_info.ret == '1') {
                console.log("数据"+remote_ip_info);
                htmlCity.city=remote_ip_info.city;//获取到当前城市
                htmlCity.setCity(htmlCity.city);//缓存当前的城市
            } else {
                alert('没有找到匹配的IP地址信息！');
                htmlCity.city="深圳";
            }
        },
        /**
         *将当前城市缓存到localStorage中
         **/
        setCity:function(city_name){
            if(window.localStorage){
                htmlCity.city=localStorage.currentCity||city_name;
                localStorage.currentCity=htmlCity.city;
                if($(".fixedCity")){
                    $(".fixedCity").html(htmlCity.city);
                }  
            }else{
                console.log("对不起，您的浏览器不支持localstorage"); 
            }  
        },
        getAddress:function(reqUrl,callback){
            $.getScript(reqUrl,callback);
        }
    }
htmlCity.getCity();
$(".hotCity-ul .h-city").on("click",function(){
    var hot_city=$(this).attr("data-city");
    $("#fixedCity").html(hot_city);
});
var url=SYS_GLOBAL_CONFIG.reqUrl,
    asyn="true",
    type="post";
var area_data=JSON.stringify({
    "code":"getDictAreaList",
    "version":"1.0",
    "data":{
        "level":"1",
        "fullName":""
    }
});

var req_area=JSON.stringify({
    "code":"getPCDictAreaList",
    "version":"1.0",
    "data":{
        "level":"root"
    }
});

function getData(url,area_data,call_func,asyn,type){
    $.ajax({
        url:url,
        type:type,
        datatype:"json",
        contentType:"application/json",
        data:area_data,
        async:asyn,
        success:function(data){
            call_func(data);  
        },
        error:function(msg){
            console.log("异常："+msg);
        }   
    });        
} 
//getData(url,area_data,call_func,asyn,type);
getData(url,req_area,call_func2,asyn,type);

function call_func2(data){
    console.log(data);
    var all_area=data.data.pda;
    var all_len=all_area.length;
    var dataStr='';
    for(var i=0;i<all_len;i++){
        var c_area=all_area[i].areas;
        var c_len=c_area.length;
        var li='';
        for(var j=0;j<c_len;j++){
            li+='<li class="a_city bor_line" data-area="'+c_area[j].areaName+'">'+c_area[j].areaName+'</li>';
        }
        dataStr+='<li class="all_li"><h3>'+all_area[i].areaName+'</h3><ul>'+li+'<div class="clears"></div></ul><div class="clears"></div></li>';
        
    }
    
    $(".all_ul").html(dataStr);
    $(".a_city,.h-city").off("click");
    $(".a_city,.h-city").on("click",function(){
           
            var curr_city;
            if($(this).attr("data-area").indexOf("北京市")>-1){
                curr_city="北京";
            }else{
                curr_city=$(this).attr("data-area");
            }

            if(window.localStorage){
                localStorage.currentCity=curr_city;
            }else{
                console.log("您的浏览器不支持");
            }
            location.href="./index.html?currentCity="+curr_city;
        });
    
       
}
$(".a_city,.h-city").off("click");
$(".a_city,.h-city").on("click",function(){
    var curr_city;
    if($(this).attr("data-area").indexOf("北京市")>-1){
        curr_city="北京";
    }else{
        curr_city=$(this).attr("data-area");
    }
    if(window.localStorage){
        localStorage.currentCity=curr_city;
    }else{
        console.log("您的浏览器不支持");
    }
location.href="./index.html?currentCity="+curr_city;
});
function call_func(data){
    console.log(data);
    var dic_area=data.data.dictAreas;
    var len=dic_area.length;
    var areaStr="";
    if(len>0){
        for(var i=0;i<len;i++){
            if(i==len-1){
                areaStr+='<li data-area="'+dic_area[i].areaName+'" class="a_city">'+dic_area[i].areaName+'</li><div class="clears"></div>';        
            }else{
                areaStr+='<li data-area="'+dic_area[i].areaName+'" class="a_city">'+dic_area[i].areaName+'</li>';
            }
        }
        $(".allCitys").html(areaStr);
        
    }
    
}
var liLength=$(".h-city").length;
for(var i=1;i<liLength+1;i++){
    
    $(".h-city").addClass("bor_line");
}